import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @author Pavel Belevich
 *         Date: Sep 18, 2012
 *         Time: 3:04:37 PM
 */
@SuppressWarnings({ "UnusedDeclaration" })
public class Deque<Item> implements Iterable<Item> {

    private static class Node<Item> {
        private Item item;
        private Node<Item> prev;
        private Node<Item> next;

        public Node(Item item, Node<Item> prev, Node<Item> next) {
            this.item = item;
            this.prev = prev;
            this.next = next;
        }
    }

    private Node<Item> first;
    private Node<Item> last;
    private int size;

    // construct an empty deque
    public Deque() {
    }

    // is the deque empty?
    public boolean isEmpty() {
        return this.first == null;
    }

    // return the number of items on the deque
    public int size() {
        return this.size;
    }

    // insert the item at the front
    public void addFirst(Item item) {
        if (item == null) throw new NullPointerException();
        Node<Item> node = new Node<Item>(item, null, this.first);
        if (this.first != null)
            this.first.prev = node;
        else
            this.last = node;
        this.first = node;
        this.size++;
    }

    // insert the item at the end
    public void addLast(Item item) {
        if (item == null) throw new NullPointerException();
        Node<Item> node = new Node<Item>(item, this.last, null);
        if (this.last != null)
            this.last.next = node;
        else
            this.first = node;
        this.last = node;
        this.size++;
    }

    // delete and return the item at the front
    public Item removeFirst() {
        Node<Item> first = this.first;
        if (first == null) throw new NoSuchElementException();
        if (first.next != null)
            first.next.prev = null;
        else
            this.last = null;
        this.first = first.next;
        first.next = null;
        this.size--;
        return first.item;
    }

    // delete and return the item at the end
    public Item removeLast() {
        Node<Item> last = this.last;
        if (last == null) throw new NoSuchElementException();
        if (last.prev != null)
            last.prev.next = null;
        else
            this.first = null;
        this.last = last.prev;
        last.prev = null;
        this.size--;
        return last.item;
    }

    private class DequeIterator implements Iterator<Item> {

        private Node<Item> curr = Deque.this.first;

        @Override
        public boolean hasNext() {
            return curr != null;
        }

        @Override
        public Item next() {
            if (curr == null) throw new NoSuchElementException();
            final Item item = curr.item;
            curr = curr.next;
            return item;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

    // return an iterator over items in order from front to end
    @Override
    public Iterator<Item> iterator() {
        return new DequeIterator();
    }

}
